package com.yzw.phonestoreservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzw.phonestoreservice.exception.PhoneException;
import com.yzw.phonestoreservice.form.AddressForm;
import com.yzw.phonestoreservice.pojo.BuyerAddress;
import com.yzw.phonestoreservice.service.BuyerAddressService;
import com.yzw.phonestoreservice.mapper.BuyerAddressMapper;
import com.yzw.phonestoreservice.vo.BuyerAddressVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author YU
* @description 针对表【buyer_address(收货地址表)】的数据库操作Service实现
* @createDate 2022-11-07 09:59:09
*/
@Service
public class BuyerAddressServiceImpl extends ServiceImpl<BuyerAddressMapper, BuyerAddress>
    implements BuyerAddressService{

    @Override
    public List<BuyerAddressVo> addressList() {
        List<BuyerAddress> buyerAddresslist = this.list();
        List<BuyerAddressVo> buyerAddressVos = buyerAddresslist.stream().map(new Function<BuyerAddress, BuyerAddressVo>() {
            @Override
            public BuyerAddressVo apply(BuyerAddress buyerAddress) {
                BuyerAddressVo buyerAddressVo = new BuyerAddressVo();
                BeanUtils.copyProperties(buyerAddress, buyerAddressVo);
                return buyerAddressVo;
            }
        }).collect(Collectors.toList());

        return buyerAddressVos;

    }

    @Override
    public Boolean buyerAddressCreate(AddressForm addressForm) {
        BuyerAddress buyerAddress = null;
        if(addressForm.getId() ==null){
            //添加操作
            buyerAddress = new BuyerAddress();
        }else{
            //修改操作
            buyerAddress = this.getById(addressForm.getId());
        }
        //把addressForm 中的数据取出来 封装到 BuyerAddress
        buyerAddress.setBuyerName(addressForm.getName());
        buyerAddress.setBuyerPhone(addressForm.getTel());

        StringBuffer sb = new StringBuffer();

        sb.append(addressForm.getProvince())
                .append(addressForm.getCity())
                .append(addressForm.getCounty())
                .append(addressForm.getAddressDetail());

        buyerAddress.setBuyerAddress(sb.toString());
        buyerAddress.setAreaCode(addressForm.getAreaCode());

        boolean save = this.saveOrUpdate(buyerAddress);
        return save;
    }

    @Override
    public Boolean buyerAddressDel(Integer addressId) {
        boolean b = this.removeById(addressId);
        return b;
    }
}




